package com.sky.web.admin;

import com.sky.dto.CategoryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //添加分类
    @PostMapping
    public Result add(@RequestBody CategoryDTO categoryDTO) {
        categoryService.add(categoryDTO);
        return Result.success();
    }

    //分类分页查询
    @GetMapping("/page")
    public Result page(CategoryDTO categoryDTO) {
        PageResult pageResult = categoryService.page(categoryDTO);
        return Result.success(pageResult);
    }

    //根据id删除
    @DeleteMapping
    public Result delete(@RequestParam Long id) {
        categoryService.deleteById(id);
        return Result.success();
    }

    //修改分类信息
    @PutMapping
    public Result update(@RequestBody Category category) {
        categoryService.update(category);
        return Result.success();

    }

    //启用禁用
    @PostMapping("/status/{status}")
    public Result startOrStop(
            @PathVariable Integer status,
            @RequestParam Long id) {
        categoryService.startOrStop(id, status);
        return Result.success();
    }

    //菜品套餐
    @GetMapping("/list")
    public Result listByType(Integer type) {
        CategoryDTO categoryDTO=CategoryDTO.builder()
                .type(type)
                .status(1)
                .build();
        //根据分类列表查询
        List<Category> list = categoryService.list(categoryDTO);
        //返回结果
        return Result.success(list);

    }
}
